package com.xebest.web.service.impl;

import com.xebest.api.dto.XeVenInquirysheetDTO;
import com.xebest.api.dto.resdto.XeVenInquirysheetResDTO;
import com.xebest.api.service.XeVenInquirysheetApiService;
import com.xebest.common.enums.erp.order.VenOrderInquirySheetStatusEnum;
import com.xebest.common.util.XeBaseUtil;
import com.xebest.web.form.XeVenInquirySheetForm;
import com.xebest.web.mapper.read.XeVenInquirysheetReadMapper;
import com.xebest.web.mapper.write.XeVenInquirysheetWriteMapper;
import com.xebest.web.model.XeVenInquirySheetEntity;
import com.xebest.web.service.XeVenInquirysheetService;
import com.xebest.web.view.ErpUserInfo;
import com.xebest.web.view.XeVenInquirySheetView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class XeVenInquirysheetServiceImpl implements XeVenInquirysheetService {

    @Autowired
    private XeVenInquirysheetReadMapper xeVenInquirysheetReadMapper;    //询价单读取
    @Autowired
    private XeVenInquirysheetWriteMapper xeVenInquirysheetWriteMapper;  //询价单业务操作
    @Autowired
    private XeBaseUtil xeBaseUtil;
    @Autowired
    private XeVenInquirysheetApiService xeVenInquirysheetApiService;



    /**
     * 询价单List信息
     * @author: Wangyp
     * @createTime: 2017/6/16 11:33
     * @history:
     * @param :venInquirySheetForm
     * @return: list
     */
    public List<XeVenInquirySheetView> queryXeVenInquirySheetList(XeVenInquirySheetForm xeVenInquirySheetForm){
        List<XeVenInquirySheetView> xeVenInquirySheetView = xeVenInquirysheetReadMapper.queryXeVenInquirySheetList(xeVenInquirySheetForm);
        for (int i = 0; i < xeVenInquirySheetView.size(); i++) {
            XeVenInquirySheetView sheetView = xeVenInquirySheetView.get(i);
            if (sheetView.getCaArea()!=null) {
                sheetView.setCaAddress(sheetView.getCaProvince()+"-"+sheetView.getCaCity()+"-"+sheetView.getCaArea());
            }else {
                sheetView.setCaAddress(sheetView.getCaProvince()+"-"+sheetView.getCaCity());
            }
        }
        return xeVenInquirySheetView;
    }

    /**
     *修改询价单信息
     *@Auther wangyp
     *@@createTime:  2017/6/14 16:55
     *@return: record
     */
    public int updateByPrimary(XeVenInquirySheetForm entity){
        return xeVenInquirysheetWriteMapper.updateByPrimary(entity);
    }

    /**
     * 根据询价单号  批量修改询价单状态
     * @author: Wangyp
     * @createTime: 2017/8/28 11:09
     * @param :
     * @return:
     */
    public int batchUpdateSheet(XeVenInquirySheetForm xeVenInquirySheetForm ) throws Exception{
        return xeVenInquirysheetWriteMapper.batchUpdateSheet(xeVenInquirySheetForm);
    }
    /**
      * 询价单成交
      * @author: Wangyp
      * @createTime: 2017/8/22 10:54
      * @param :
      * @return:
      */
    @Override
    @Transactional(value="writeTransactionManager")
    public int updateSheetDeal(XeVenInquirySheetForm form ,ErpUserInfo userInfo)throws Exception{
        int result =0;
        Date date = new Date();
        //拼装参数
        form.setUserCode(userInfo.getUserCode());
        form.setPriceDate(date);   //成交时间
        form.setInquiryStatus(VenOrderInquirySheetStatusEnum.YIBJ.getValue());    //询价单状态   1、 已报价
        form.setModifyUser(userInfo.getLoginName());
        form.setAgreePrice(form.getAgreePrice());
        form.setOperUser(userInfo.getLoginName());
        form.setInquirysheetCode(form.getInquirysheetCode());
        BigDecimal totalPrice= form.getAgreePrice().multiply(new BigDecimal(form.getProductCount()));//商品总价
        form.setTotalPrice(totalPrice);
        //询价单同意
        result =xeVenInquirysheetWriteMapper.updateByPrimary(form);
        if (result<=0){
          throw  new  RuntimeException();
        }
        //同步询价信息
        XeVenInquirysheetDTO dto = new XeVenInquirysheetDTO();
        dto.setUserCode(userInfo.getUserCode());
        dto.setPriceDate(date);
        dto.setOperuser(userInfo.getLoginName());
        dto.setInquirysheetCode(form.getInquirysheetCode());
        dto.setAgreePrice(form.getAgreePrice());
        dto.setInquiryStatus(VenOrderInquirySheetStatusEnum.YIBJ.getValue());//询价单成交

        dto.setTotalPrice(totalPrice);
        if (result>0){
            XeVenInquirysheetResDTO  dto1= xeVenInquirysheetApiService.synMerchantInfo(dto);
            if (!dto1.getStatus().equals("2000")){
                throw  new  RuntimeException();
            }
        }
        return result;


    }

    /**
      * 询价单驳回
      * @author: Wangyp
      * @createTime: 2017/8/22 11:05
      * @param :
      * @return:
      */
    @Override
    @Transactional(value="writeTransactionManager")
    public int updateSheetCancel(XeVenInquirySheetForm form ,ErpUserInfo userInfo){
        int result =0;
        Date date = new Date();
        //拼装参数
        form.setUserCode(userInfo.getUserCode());
        form.setInquiryStatus(VenOrderInquirySheetStatusEnum.YIBH.getValue());    //询价单状态
        form.setModifyUser(userInfo.getLoginName());
        form.setOperUser(userInfo.getLoginName());
        form.setRemark(form.getRemark());
        form.setInquirysheetCode(form.getInquirysheetCode());
        //询价单驳回
        result =xeVenInquirysheetWriteMapper.updateByPrimary(form);
        if (result<=0){
            throw  new  RuntimeException();
        }
        //同步询价信息
        XeVenInquirysheetDTO dto = new XeVenInquirysheetDTO();
        dto.setUserCode(userInfo.getUserCode());
        dto.setOperuser(userInfo.getLoginName());
        dto.setInquirysheetCode(form.getInquirysheetCode());
        dto.setRejectReason(form.getRemark());
        dto.setInquiryStatus(VenOrderInquirySheetStatusEnum.YIBH.getValue());  //询价单驳回
        if (result>0){
            XeVenInquirysheetResDTO  dto1= xeVenInquirysheetApiService.synMerchantInfo(dto);
            if (!dto1.getStatus().equals("2000")){
                throw  new  RuntimeException();
            }
        }
        return result;

    }

    /**
     * 单个插入询价单  用于询价单同步接口使用
     * @author: 李贝贝
     * @createTime: 2017年6月26日 上午11:09:07  
     * @history:  
     * @param entity
     * @return int
     */
    public int insertInquirySheet(XeVenInquirySheetEntity entity) throws Exception{
    	return xeVenInquirysheetWriteMapper.insertInquirySheet(entity);
    }

    /**
     * 根据询价单号查询询价单信息
     * @author: Wangyp
     * @createTime: 2017/8/18 10:46
     * @param :
     * @return:
     */
   public XeVenInquirySheetView queryXeVenInquiryByCode(XeVenInquirySheetEntity entity){
        return xeVenInquirysheetReadMapper.queryXeVenInquiryByCode(entity);
    }

    /**
     * 批量插入询价单  用于询价单同步接口使用
     * @author: Wangyp
     * @createTime: 2017/8/22 16:01
     * @param :
     * @return:
     */
   public int batchInsertInquirySheet( List<XeVenInquirySheetEntity> entityList)throws Exception{
        return xeVenInquirysheetWriteMapper.batchInsertInquirySheet(entityList);
    }

    /**
     * 批量修改询价单
     * @author: Wangyp
     * @createTime: 2017/9/1 16:08
     * @param :
     * @return:
     */
    public int batchUpdateInquiry( List<XeVenInquirySheetEntity> entityList)throws Exception{
        return xeVenInquirysheetWriteMapper.batchUpdateInquiry(entityList);
    }

    public  XeVenInquirySheetView listXeVenInquiryByCode(String inquirysheetCode){
        return xeVenInquirysheetWriteMapper.listXeVenInquiryByCode(inquirysheetCode);
    }

}
